<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Utility_Widgets_AccessCounter_Widget extends Tomato_Widget {

    protected function _prepareShow() {
        try {
            //khởi tạo cache
            $frontendOptions = array(
                'lifetime' => null, // cache lifetime forever
                'automatic_serialization' => true
            );

            $backendOptions = array(
                'cache_dir' => TOMATO_TEMP_DIR . DS . 'cache' . DS // Directory where to put the cache files
            );
            // getting a Zend_Cache_Core object
            $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
            $cacheToday = Zend_Cache::factory('Core', 'File', array('lifetime' => 86400, 'automatic_serialization' => true ), $backendOptions);
            $access = $cache->load('access');
            $accessToday = $cacheToday->load('access_today');
            $this->_view->access = 0;
            $this->_view->accessToday = 0;
            if ($access === false) {
                $cache->save(0, 'access');
            } else {
                $this->_view->access = $access;
            }
            if($accessToday === false){
                $cacheToday->save(0, 'access_today');
            }
            $session = new Zend_Session_Namespace();
            if (!isset($session->access)) {
                $this->_view->access++;
                $cache->save($this->_view->access, 'access');
            }
            if(!isset($session->accessToday)){
                $this->_view->accessToday++;
                $cache->save($this->_view->accessToday, 'access_today');
            }
            //đếm số người online
            $onlineInfos = $cache->load('online');
            $this->_view->online = 0;
            $onlineInfo = new stdClass();
            if (isset($session->onlineInfo)) {
                $onlineInfo = $session->onlineInfo;
            } else {
                $onlineInfo->ip = $_SERVER['REMOTE_ADDR'];
                $onlineInfo->time = time();
                $onlineInfo->local = (strpos('admin', $_SERVER['PHP_SELF'])) ? 'admin' : 'site';
                $session->onlineInfo = $onlineInfo;
            }
            if ($onlineInfos === false) {
                $onlineInfos = array();
                $onlineInfos[$onlineInfo->ip] = $onlineInfo;
                $cache->save($onlineInfos, 'online');
            } else {
                if (key_exists($onlineInfo->ip, $onlineInfos)) {
                    $onlineInfos[$onlineInfo->ip]->time = time();
                } else {
                    $onlineInfos[$onlineInfo->ip] = $onlineInfo;
                }
                foreach ($onlineInfos as $oi) {
                    if (time() - $oi->time > 900) {
                        unset($oi);
                    }
                }
                $cache->save($onlineInfos, 'online');
            }
            $this->_view->online += count($onlineInfos);
           
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
    }

    protected function _prepareConfig() {
        
    }

}

?>
